package com.simple.weibo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.simple.weibo.bean.User;
import com.simple.weibo.database.SimpleDatabase;
import com.simple.weibo.database.SimpleDatabaseHelper;
import com.simple.weibo.oauth.OAuth;

public class OAuthActivity extends Activity {
	OAuth oauth ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.oauth_view);
		super.onCreate(savedInstanceState);
		 
		
		WebView wv = (WebView)findViewById(R.id.web);
		WebSettings ws = wv.getSettings();
		ws.setJavaScriptEnabled(true);
		ws.setBuiltInZoomControls(true);
		wv.setWebChromeClient(new WebChromeClient(){

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				
				super.onProgressChanged(view, newProgress);
				OAuthActivity.this.setProgress(newProgress*100);
			}
			
		});
		
		oauth= new OAuth();
		oauth.requestAccessToken(this,wv);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		
		super.onNewIntent(intent);
		User user = oauth.getAccessToken(intent);
		if(user != null){
			SimpleDatabaseHelper helper = new SimpleDatabaseHelper(this, "mydatabase", null, 1);
			SimpleDatabase db = new SimpleDatabase(helper);
			db.addUser(user);
            db.close();
			
			Intent it = new Intent();
			it.putExtra("user", user);
			it.setClass(OAuthActivity.this, WeiboMainActivity.class);
			startActivity(it);
			
		}else{
			Intent it = new Intent();
			intent.setClass(OAuthActivity.this, MainActivity.class);
			startActivity(it);
		}
		finish();
		
		
	}
	
	

}
